<!DOCTYPE html>
<html>
<!--
Copyright 2006 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by an Apache 2.0 License.
See the COPYING file for details.
-->
<head>
<title>Closure Unit Tests - goog.locale.NumberFormat</title>
<script type="text/javascript" src="../base.js"></script>
<script type="text/javascript">
  goog.require('goog.locale');
  goog.require('goog.locale.formatting');
  goog.require('goog.locale.NumberFormat');
  goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>
  var NumberFormatConstants_fr = {
   DECIMAL_SEP:',',
   GROUP_SEP:'\u00A0',
   PERCENT:'%',
   ZERO_DIGIT:'0',
   PLUS_SIGN:'+',
   MINUS_SIGN:'-',
   EXP_SYMBOL:'E',
   PERMILL:'\u2030',
   INFINITY:'\u221E',
   NAN:'NaN',
   MONETARY_SEP:',',
   MONETARY_GROUP_SEP:'\u00A0',
   DECIMAL_PATTERN:'#,##0.###',
   SCIENTIFIC_PATTERN:'#E0',
   PERCENT_PATTERN:'#,##0\u00A0%',
   CURRENCY_PATTERN:'#,##0.00 \u00A4',
   DEF_CURRENCY_CODE:'EUR'
  };
  registerNumberFormatConstants(NumberFormatConstants_fr, 'fr');
  var NumberFormatConstants_en = {
   DECIMAL_SEP:'.',
   GROUP_SEP:',',
   PERCENT:'%',
   ZERO_DIGIT:'0',
   PLUS_SIGN:'+',
   MINUS_SIGN:'-',
   EXP_SYMBOL:'E',
   PERMILL:'\u2030',
   INFINITY:'\u221E',
   NAN:'NaN',
   MONETARY_SEP:'.',
   MONETARY_GROUP_SEP:',',
   DECIMAL_PATTERN:'#,##0.###',
   SCIENTIFIC_PATTERN:'#E0',
   PERCENT_PATTERN:'#,##0%',
   CURRENCY_PATTERN:'\u00A4#,##0.00',
   DEF_CURRENCY_CODE:'USD'
  };

  registerNumberFormatConstants(NumberFormatConstants_en, 'en');

function setUp() {
  goog.locale.setLocale('en');
}

function veryBigNumberCompare(str1, str2) {
  return str1.length == str2.length &&
         str1.substring(0, 8) == str2.substring(0, 8);
}

function testVeryBigNumber() {
  var str;
  str = goog.locale.standardFormatNumber(goog.locale.CURRENCY_PATTERN,
                                    1785599999999999888888888888888);
  // when comparing big number, various platform have small different in
  // precision. We have to tolerate that using veryBigNumberCompare.
  var expected = "$1,785,599,999,999,999,400,000,000,000,000.00";
  assertTrue(veryBigNumberCompare("$1,785,599,999,999,999,400,000,000,000,000.00",
             str));
  str = goog.locale.standardFormatNumber(goog.locale.CURRENCY_PATTERN, 1.7856E30);
  assertTrue(veryBigNumberCompare("$1,785,599,999,999,999,400,000,000,000,000.00",
             str));
  str = goog.locale.standardFormatNumber(goog.locale.CURRENCY_PATTERN, 1.3456E20);
  assertTrue(veryBigNumberCompare("$134,560,000,000,000,000,000.00", str));
  str = goog.locale.standardFormatNumber(goog.locale.DECIMAL_PATTERN, 1.3456E20);
  assertTrue(veryBigNumberCompare("134,559,999,999,999,980,000", str));
  str = goog.locale.standardFormatNumber(goog.locale.PERCENT_PATTERN, 1.3456E20);
  assertTrue(veryBigNumberCompare("13,456,000,000,000,000,000,000%", str));
  str = goog.locale.standardFormatNumber(goog.locale.SCIENTIFIC_PATTERN, 1.3456E20);
  //assertEquals("1.346E20", str);
  assertEquals("1E20", str);
}

function testStandardFormat() {
  var str;
  str = goog.locale.standardFormatNumber(goog.locale.CURRENCY_PATTERN, 1234.579);
  assertEquals("$1,234.58", str);
  str = goog.locale.standardFormatNumber(goog.locale.DECIMAL_PATTERN, 1234.579);
  assertEquals("1,234.579", str);
  str = goog.locale.standardFormatNumber(goog.locale.PERCENT_PATTERN, 1234.579);
  assertEquals("123,458%", str);
  str = goog.locale.standardFormatNumber(goog.locale.SCIENTIFIC_PATTERN, 1234.579);
  //assertEquals("1.235E3", str);
  assertEquals("1E3", str);
}

function testNegativePercentage() {
  var str;
  str = goog.locale.formatNumber("#,##0.00%", -1234.56);
  assertEquals("-123,456.00%", str);

  str = goog.locale.standardFormatNumber(goog.locale.PERCENT_PATTERN,
                                         -1234.579);
  assertEquals("-123,458%", str);
}

function testBasicParse() {
  var value;

  value = goog.locale.parseNumber("0.0000", "123.4579");
  assertEquals(123.4579, value);

  value = goog.locale.parseNumber("0.0000", "+123.4579");
  assertEquals(123.4579, value);

  value = goog.locale.parseNumber("0.0000", "-123.4579");
  assertEquals(-123.4579, value);
}

function testPrefixParse() {
  var value;

  value = goog.locale.parseNumber("0.0;(0.0)", "123.4579");
  assertEquals(123.4579, value);

  value = goog.locale.parseNumber("0.0;(0.0)", "(123.4579)");
  assertEquals(-123.4579, value);
}

function testPrecentParse() {
  var value;

  value = goog.locale.parseNumber("0.0;(0.0)", "123.4579%");
  assertEquals((123.4579/100), value);

  value = goog.locale.parseNumber("0.0;(0.0)", "(%123.4579)");
  assertEquals((-123.4579/100), value);

  value = goog.locale.parseNumber("0.0;(0.0)", "123.4579\u2030");
  assertEquals((123.4579/1000), value);

  value = goog.locale.parseNumber("0.0;(0.0)", "(\u2030123.4579)");
  assertEquals((-123.4579/1000), value);
}

function testPercentAndPerMillAdvance() {
  var value;
  var pos = [0];
  value = goog.locale.parseNumber("0", "120%", pos);
  assertEquals(1.2, value);
  assertEquals(4, pos[0]);
  pos[0] = 0;
  value = goog.locale.parseNumber("0", "120\u2030", pos);
  assertEquals(0.12, value);
  assertEquals(4, pos[0]);
}

function testInfinityParse() {
  var value;

  // gwt need to add those symbols first
  value = goog.locale.parseNumber("0.0;(0.0)", "\u221e");
  assertEquals(Number.POSITIVE_INFINITY, value);

  value = goog.locale.parseNumber("0.0;(0.0)", "(\u221e)");
  assertEquals(Number.NEGATIVE_INFINITY, value);
}
function testExponentParse() {
  var value;

  value = goog.locale.parseNumber("#E0", "1.234E3");
  assertEquals(1.234E+3, value);

  value = goog.locale.parseNumber("0.###E0", "1.234E3");
  assertEquals(1.234E+3, value);

  value = goog.locale.parseNumber("#E0", "1.2345E4");
  assertEquals(12345.0, value);

  value = goog.locale.parseNumber("0E0", "1.2345E4");
  assertEquals(12345.0, value);

  value = goog.locale.parseNumber("0E0", "1.2345E+4");
  assertEquals(12345.0, value);
}

function testGroupingParse() {
  var value;

  value = goog.locale.parseNumber("#,###", "1,234,567,890");
  assertEquals(1234567890, value);
  value = goog.locale.parseNumber("#,####", "12,3456,7890");
  assertEquals(1234567890, value);

  value = goog.locale.parseNumber("#", "1234567890");
  assertEquals(1234567890, value);
}

/**
 * Add as many tests as you like.
 */
function testBasicFormat() {
  var str = goog.locale.formatNumber("0.0000", 123.45789179565757);
  assertEquals("123.4579", str);
}

function testGrouping() {
  var str;

  str = goog.locale.formatNumber("#,###", 1234567890);
  assertEquals("1,234,567,890", str);
  str = goog.locale.formatNumber("#,####", 1234567890);
  assertEquals("12,3456,7890", str);

  str = goog.locale.formatNumber("#", 1234567890);
  assertEquals("1234567890", str);
}

function testPerMill() {
  var str;

  str = goog.locale.formatNumber("###.###\u2030", 0.4857);
  assertEquals("485.7\u2030", str);
}

function testCurrency() {
  var str;

  str = goog.locale.formatNumber("\u00a4#,##0.00;-\u00a4#,##0.00", 1234.56);
  assertEquals("$1,234.56", str);
  str = goog.locale.formatNumber("\u00a4#,##0.00;-\u00a4#,##0.00", -1234.56);
  assertEquals("-$1,234.56", str);

  str = goog.locale.formatNumber("\u00a4\u00a4 #,##0.00;-\u00a4\u00a4 #,##0.00", 1234.56);
  assertEquals("USD 1,234.56", str);
  str = goog.locale.formatNumber("\u00a4\u00a4 #,##0.00;\u00a4\u00a4 -#,##0.00", -1234.56);
  assertEquals("USD -1,234.56", str);

  var formatter = goog.locale.getNumberFormatter("\u00a4#,##0.00;-\u00a4#,##0.00", "BRL");
  str = formatter.format(1234.56);
  assertEquals("R$1,234.56", str);
  str = formatter.format(-1234.56);
  assertEquals("-R$1,234.56", str);

  formatter = goog.locale.getNumberFormatter("\u00a4\u00a4 #,##0.00;(\u00a4\u00a4 #,##0.00)", "BRL");
  str = formatter.format(1234.56);
  assertEquals("BRL 1,234.56", str);
  str = formatter.format(-1234.56);
  assertEquals("(BRL 1,234.56)", str);
}

function testQuotes() {
  var str;

  str = goog.locale.formatNumber("a'fo''o'b#", 123);
  assertEquals("afo'ob123", str);

  str = goog.locale.formatNumber("a''b#", 123);
  assertEquals("a'b123", str);

  str = goog.locale.formatNumber("a'fo''o'b#", -123);
  assertEquals("afo'ob-123", str);

  str = goog.locale.formatNumber("a''b#", -123);
  assertEquals("a'b-123", str);
}

function testZeros() {
  var str;

  str = goog.locale.formatNumber("#.#", 0);
  assertEquals("0", str);
  str = goog.locale.formatNumber("#.", 0);
  assertEquals("0.", str);
  str = goog.locale.formatNumber(".#", 0);
  assertEquals(".0", str);
  str = goog.locale.formatNumber("#", 0);
  assertEquals("0", str);

  str = goog.locale.formatNumber("#0.#", 0);
  assertEquals("0", str);
  str = goog.locale.formatNumber("#0.", 0);
  assertEquals("0.", str);
  str = goog.locale.formatNumber("#.0", 0);
  assertEquals(".0", str);
  str = goog.locale.formatNumber("#", 0);
  assertEquals("0", str);
  str = goog.locale.formatNumber("000", 0);
  assertEquals("000", str);
}

function testExponential() {
  var str;

  str = goog.locale.formatNumber("0.####E0", 0.01234);
  assertEquals("1.234E-2", str);
  str = goog.locale.formatNumber("00.000E00", 0.01234);
  assertEquals("12.340E-03", str);
  str = goog.locale.formatNumber("##0.######E000", 0.01234);
  assertEquals("12.34E-003", str);
  str = goog.locale.formatNumber("0.###E0;[0.###E0]", 0.01234);
  assertEquals("1.234E-2", str);

  str = goog.locale.formatNumber("0.####E0", 123456789);
  assertEquals("1.2346E8", str);
  str = goog.locale.formatNumber("00.000E00", 123456789);
  assertEquals("12.346E07", str);
  str = goog.locale.formatNumber("##0.######E000", 123456789);
  assertEquals("123.456789E006", str);
  str = goog.locale.formatNumber("0.###E0;[0.###E0]", 123456789);
  assertEquals("1.235E8", str);

  str = goog.locale.formatNumber("0.####E0", 1.23e300);
  assertEquals("1.23E300", str);
  str = goog.locale.formatNumber("00.000E00", 1.23e300);
  assertEquals("12.300E299", str);
  str = goog.locale.formatNumber("##0.######E000", 1.23e300);
  assertEquals("1.23E300", str);
  str = goog.locale.formatNumber("0.###E0;[0.###E0]", 1.23e300);
  assertEquals("1.23E300", str);

  str = goog.locale.formatNumber("0.####E0", -3.141592653e-271);
  assertEquals("-3.1416E-271", str);
  str = goog.locale.formatNumber("00.000E00", -3.141592653e-271);
  assertEquals("-31.416E-272", str);
  str = goog.locale.formatNumber("##0.######E000", -3.141592653e-271);
  assertEquals("-314.159265E-273", str);
  str = goog.locale.formatNumber("0.###E0;[0.###E0]", -3.141592653e-271);
  assertEquals("[3.142E-271]", str);

  str = goog.locale.formatNumber("0.####E0", 0);
  assertEquals("0E0", str);
  str = goog.locale.formatNumber("00.000E00", 0);
  assertEquals("00.000E00", str);
  str = goog.locale.formatNumber("##0.######E000", 0);
  assertEquals("0E000", str);
  str = goog.locale.formatNumber("0.###E0;[0.###E0]", 0);
  assertEquals("0E0", str);

  str = goog.locale.formatNumber("0.####E0", -1);
  assertEquals("-1E0", str);
  str = goog.locale.formatNumber("00.000E00", -1);
  assertEquals("-10.000E-01", str);
  str = goog.locale.formatNumber("##0.######E000", -1);
  assertEquals("-1E000", str);
  str = goog.locale.formatNumber("0.###E0;[0.###E0]", -1);
  assertEquals("[1E0]", str);

  str = goog.locale.formatNumber("0.####E0", 1);
  assertEquals("1E0", str);
  str = goog.locale.formatNumber("00.000E00", 1);
  assertEquals("10.000E-01", str);
  str = goog.locale.formatNumber("##0.######E000", 1);
  assertEquals("1E000", str);
  str = goog.locale.formatNumber("0.###E0;[0.###E0]", 1);
  assertEquals("1E0", str);

  str = goog.locale.formatNumber("#E0", 12345.0);
  //assertEquals(".1E5", str);
  assertEquals("1E4", str);
  str = goog.locale.formatNumber("0E0", 12345.0);
  assertEquals("1E4", str);
  str = goog.locale.formatNumber("##0.###E0", 12345.0);
  assertEquals("12.345E3", str);
  str = goog.locale.formatNumber("##0.###E0", 12345.00001);
  assertEquals("12.345E3", str);
  str = goog.locale.formatNumber("##0.###E0",  12345);
  assertEquals("12.345E3", str);

  str = goog.locale.formatNumber("##0.####E0",  789.12345e-9);
  assertEquals("789.1235E-9", str);
  str = goog.locale.formatNumber("##0.####E0",  780.e-9);
  assertEquals("780E-9", str);
  str = goog.locale.formatNumber(".###E0",  45678.0);
  assertEquals(".457E5", str);
  str = goog.locale.formatNumber(".###E0",  0);
  assertEquals(".0E0", str);

  str = goog.locale.formatNumber("#E0",  45678000);
  assertEquals("5E7", str);
  str = goog.locale.formatNumber("##E0",  45678000);
  assertEquals("46E6", str);
  str = goog.locale.formatNumber("####E0",  45678000);
  assertEquals("4568E4", str);
  str = goog.locale.formatNumber("0E0",  45678000);
  assertEquals("5E7", str);
  str = goog.locale.formatNumber("00E0",  45678000);
  assertEquals("46E6", str);
  str = goog.locale.formatNumber("000E0",  45678000);
  assertEquals("457E5", str);
  str = goog.locale.formatNumber("###E0",  0.0000123);
  assertEquals("12E-6", str);
  str = goog.locale.formatNumber("###E0",  0.000123);
  assertEquals("123E-6", str);
  str = goog.locale.formatNumber("###E0",  0.00123);
  assertEquals("1E-3", str);
  str = goog.locale.formatNumber("###E0",  0.0123);
  assertEquals("12E-3", str);
  str = goog.locale.formatNumber("###E0",  0.123);
  assertEquals("123E-3", str);
  str = goog.locale.formatNumber("###E0",  1.23);
  assertEquals("1E0", str);
  str = goog.locale.formatNumber("###E0",  12.3);
  assertEquals("12E0", str);
  str = goog.locale.formatNumber("###E0",  123.0);
  assertEquals("123E0", str);
  str = goog.locale.formatNumber("###E0",  1230.0);
  assertEquals("1E3", str);
}

function testGroupingParse2() {
  var value;

  value = goog.locale.parseNumber("#,###", "1,234,567,890");
  assertEquals(1234567890, value);
  value = goog.locale.parseNumber("#,####", "12,3456,7890");
  assertEquals(1234567890, value);

  value = goog.locale.parseNumber("#", "1234567890");
  assertEquals(1234567890, value);
}

function testApis() {
  var fmt;
  var str;

  fmt = goog.locale.getNumberFormatter("#,###");
  str = fmt.format(1234567890);
  assertEquals("1,234,567,890", str);

  fmt = goog.locale.getNumberFormatter("\u00a4#,##0.00;-\u00a4#,##0.00");
  str = fmt.format(1234.56);
  assertEquals("$1,234.56", str);
  fmt = goog.locale.getNumberFormatter("\u00a4#,##0.00;(\u00a4#,##0.00)");
  str = fmt.format(-1234.56);
  assertEquals("($1,234.56)", str);

  fmt = goog.locale.getNumberFormatter("\u00a4#,##0.00;-\u00a4#,##0.00", "SEK");
  str = fmt.format(1234.56);
  assertEquals("kr1,234.56", str);
  fmt = goog.locale.getNumberFormatter("\u00a4#,##0.00;(\u00a4#,##0.00)", "SEK");
  str = fmt.format(-1234.56);
  assertEquals("(kr1,234.56)", str);
}

function testLocaleSwitch() {
  goog.locale.setLocale('fr');

  var fmt = goog.locale.getNumberFormatter("#,###");
  var str = fmt.format(1234567890);
  assertEquals("1\u00a0234\u00a0567\u00a0890", str);

  fmt = goog.locale.getNumberFormatter("\u00a4#,##0.00;-\u00a4#,##0.00");
  str = fmt.format(1234.56);
  assertEquals("\u20AC1\u00a0234,56", str);
  fmt = goog.locale.getNumberFormatter("\u00a4#,##0.00;(\u00a4#,##0.00)");
  str = fmt.format(-1234.56);
  assertEquals("(\u20AC1\u00a0234,56)", str);

  fmt = goog.locale.getNumberFormatter("\u00a4#,##0.00;-\u00a4#,##0.00", "SEK");
  str = fmt.format(1234.56);
  assertEquals("kr1\u00a0234,56", str);
  fmt = goog.locale.getNumberFormatter("\u00a4#,##0.00;(\u00a4#,##0.00)", "SEK");
  str = fmt.format(-1234.56);
  assertEquals("(kr1\u00a0234,56)", str);

}

function testFrenchParse() {
  goog.locale.setLocale('fr');

  var value = goog.locale.parseNumber("0.0000", "0,30");
  assertEquals(0.30, value);

  value = goog.locale.standardParseNumber(goog.locale.CURRENCY_PATTERN, "0,30 \u20AC");
  assertEquals(0.30, value);

  value = goog.locale.parseNumber("#,##0.00", "123 456,99");
  assertEquals(123456.99, value);

  value = goog.locale.parseNumber("#,##0.00", "123\u00a0456,99");
  assertEquals(123456.99, value);

  value = goog.locale.parseNumber("#,##0.00", "8 123\u00a0456,99");
  assertEquals(8123456.99, value);
}

function testFailParseShouldReturnNaN() {
  goog.locale.setLocale('en');

  var value = goog.locale.parseNumber("0.0000", "x30");
  assertNaN(value);

  value = goog.locale.parseNumber("0.0000x", "3y");
  assertNaN(value);

  value = goog.locale.parseNumber("x0.0000x", "y3");
  assertNaN(value);
}

</script>
</body>
</html>
